package com.link.play.fit.ai.ui.guide.ac

import android.animation.Animator
import android.animation.Animator.AnimatorListener
import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.content.Intent
import android.graphics.SurfaceTexture
import android.media.MediaPlayer
import android.os.Bundle
import android.util.Log
import android.view.Surface
import android.view.TextureView.SurfaceTextureListener
import android.view.View
import androidx.core.animation.addListener
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.link.play.fit.ai.R
import com.link.play.fit.ai.base.BaseActivity
import com.link.play.fit.ai.databinding.AcGuideVideoBinding
import com.link.play.fit.ai.utils.ScreenUtil
import com.link.play.fit.ai.utils.ScreenUtil.Companion.dpToPx


class GuideVideoAc : BaseActivity<AcGuideVideoBinding>(AcGuideVideoBinding::inflate) {

    override fun useDefaultImmersive() {
        ViewCompat.setOnApplyWindowInsetsListener(binding.root) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, 0, systemBars.right, systemBars.bottom)
            insets
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStatusBarColor(isLight = false)
        playVideo()
    }

    private fun playVideo() {
//
//        // 从 res/raw 加载视频
//        val videoPath = "android.resource://" + packageName + "/" + R.raw.scan
//        binding.videoView.setVideoURI(Uri.parse(videoPath))
//
//
//        // 设置播放控制条（需 MediaController）
//        val mediaController = android.widget.MediaController(this)
//        mediaController.setAnchorView(binding.videoView)
//        binding.videoView.setMediaController(mediaController)
//
//
//        // 开始播放
//        binding.videoView.start()

        val mediaPlayer: MediaPlayer? = MediaPlayer.create(this, com.link.play.fit.ai.R.raw.scan)

        binding.textureView.surfaceTextureListener = object : SurfaceTextureListener {

            override fun onSurfaceTextureAvailable(surface: SurfaceTexture, width: Int, height: Int) {
                val pHeight = ScreenUtil.getScreenHeight()
                val vHeight = mediaPlayer?.videoHeight
                val layoutParams = binding.textureView.layoutParams

//                if(vHeight>=pHeight){
//                    layoutParams.height = (vHeight)
//                    layoutParams.width = (vHeight)
//                }else{
//                    layoutParams.height = (pHeight)
//                    layoutParams.width = (pHeight)
//                }
                layoutParams.height = (pHeight)
                layoutParams.width = (pHeight)
                binding.textureView.layoutParams = layoutParams
                mediaPlayer?.setSurface(Surface(surface))
                mediaPlayer?.start()
            }

            override fun onSurfaceTextureSizeChanged(surface: SurfaceTexture, width: Int, height: Int) {
                Log.d("glc", "onSurfaceTextureSizeChanged width" + width)
                Log.d("glc", "onSurfaceTextureSizeChanged height" + height)
            }

            override fun onSurfaceTextureDestroyed(surface: SurfaceTexture): Boolean {
                return false
            }

            override fun onSurfaceTextureUpdated(surface: SurfaceTexture) {
            }
        }

        animation()

    }

    fun animation() {
        val offset = dpToPx(60f)
        val animator1 = ObjectAnimator.ofFloat(binding.tv1, "translationY", offset, 0f)
        animator1.duration = 800 // 动画持续时间（毫秒）

        val animator2 = ObjectAnimator.ofFloat(binding.tv1, "alpha", 0.1f, 1.0f)
        animator2.duration = 800 // 动画持续时间（毫秒）

        val animator3 = ObjectAnimator.ofFloat(binding.tv1, "alpha", 1.0f, 0.0f)
        animator3.duration = 500 // 动画持续时间（毫秒）


        val animator4 = ObjectAnimator.ofFloat(binding.tv2, "translationY", offset, 0f)
        animator1.duration = 800 // 动画持续时间（毫秒）

        val animator5 = ObjectAnimator.ofFloat(binding.tv2, "alpha", 0.1f, 1.0f)
        animator2.duration = 800 // 动画持续时间（毫秒）


        val animatorSet = AnimatorSet()

        val delayedStart = AnimatorSet().apply {
            startDelay = 800  // 1500ms delay
            playTogether(animator1, animator2)
        }

        animatorSet.play(delayedStart)
        animatorSet.play(animator3).after(2500)

        animatorSet.play(animator4).after(3500)
        animatorSet.play(animator5).after(3500)

        animatorSet.addListener(onEnd = {
            binding.root.postDelayed(
                {
                    binding.cl1.visibility = View.INVISIBLE
                    binding.cl2.visibility = View.VISIBLE
                    animation2()
                }, 800
            )

        })
        animatorSet.start()

    }

    fun animation2() {

        val offset = dpToPx(60f)

        val animator1 = ObjectAnimator.ofFloat(binding.tv3, "translationY", offset, 0f)
        animator1.duration = 800 // 动画持续时间（毫秒）

        val animator2 = ObjectAnimator.ofFloat(binding.tv3, "alpha", 0.1f, 1.0f)
        animator2.duration = 800 // 动画持续时间（毫秒）

        val scaleAnimatorX = ObjectAnimator.ofFloat(binding.iv, "scaleX", 1.0f, 1.05f)
        val scaleAnimatorY = ObjectAnimator.ofFloat(binding.iv, "scaleY", 1.0f, 1.05f)
        scaleAnimatorX.duration = 800
        scaleAnimatorY.duration = 800

        val animatorSet = AnimatorSet()
        animatorSet.startDelay = 1200
        animatorSet.playTogether(animator1, animator2, scaleAnimatorX, scaleAnimatorY)

        animatorSet.addListener(onEnd = {
            binding.root.postDelayed({
                startActivity(Intent(this@GuideVideoAc, GuidePbAc::class.java))
                overridePendingTransition(R.anim.fade_in, R.anim.fade_out)
                finish()
            }, 1000)
        })
        animatorSet.start()

        binding.lottie.addAnimatorListener(object : AnimatorListener {
            override fun onAnimationStart(animation: Animator) {

            }

            override fun onAnimationEnd(animation: Animator) {
            }

            override fun onAnimationCancel(animation: Animator) {
            }

            override fun onAnimationRepeat(animation: Animator) {
            }
        })


    }
}